package com.ziki.expandable;

import java.util.ArrayList;
import java.util.List;

import com.ziki.testmodulename.R;

import android.app.ActionBar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.View;
import android.widget.ExpandableListView;

public class ExpandableActivity extends AppCompatActivity {

    private static final String TAG = "AAA";
    private List<String> groupList;
    private List<List<String>> childList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        android.support.v7.app.ActionBar supportActionBar = getSupportActionBar();
        Log.d(TAG, "onCreate: " + supportActionBar);
        if (supportActionBar != null) {
            supportActionBar.setDisplayHomeAsUpEnabled(true);
            supportActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            supportActionBar.setCustomView(R.layout.title);
        }
        setContentView(R.layout.activity_expandable);
        initView();
    }

    private void initView() {
        ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
        expandableListView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
            @Override
            public void onCreateContextMenu(ContextMenu contextMenu, View view,
                    ContextMenu.ContextMenuInfo contextMenuInfo) {
                contextMenu.setHeaderTitle("AAA");
                contextMenu.add(0, 1, 0, "a");
                contextMenu.add(0, 2, 0, "b");
                contextMenu.add(0, 3, 0, "c");
            }
        });
        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                int groupCount = expandableListView.getExpandableListAdapter().getGroupCount();
                for (int j = 0; j < groupCount; j++) {
                    if (i == j) {
                        if (expandableListView.isGroupExpanded(i)) {
                            expandableListView.collapseGroup(i);
                        } else {
                            expandableListView.expandGroup(i, false);
                        }
                    } else {
                        expandableListView.collapseGroup(j);
                    }
                }

                return true;
            }
        });

        groupList = new ArrayList<>();
        childList = new ArrayList<>();
        addData("幼稚园同学", new String[] { "周杰伦", "江一燕 ", "佟丽娅", "高圆圆", "刘诗诗", "刘亦菲", "angleBaby", "张静初", "张含韵", });
        addData("小学同学", new String[] { "光头强", "熊大", "熊二", "妙蛙种子", "比卡丘", "双蛋瓦斯", "贪吃蛇" });
        addData("初中同学", new String[] { "苍井空", "小泽玛利亚", "吉泽明步", "波多野结衣", "爱川美里菜", "小川阿佐美", "桃谷绘里香", "泷泽萝拉", "北原多香子",
                "石川施恩惠", "北条麻妃", "麻仓优", "羽田爱", "保坂绘里" });
        addData("高中同学", new String[] { "习近平", "胡锦涛", "江泽民", "毛泽东", "秦始皇", "李世民", "武则天", "曹操", "刘备", "孙权" });

        expandableListView.setAdapter(new ActivityExpandableAdapter(this, groupList, childList));
    }

    private void addData(String group, String[] friend) {
        groupList.add(group);
        List<String> childitem = new ArrayList<>();
        for (int i = 0; i < friend.length; i++) {
            childitem.add(friend[i]);
        }
        childList.add(childitem);
    }
}
